﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using JustinAngel.Neurons.Framework;
using System.Linq;
using JustinAngel.Neurons.Framework.Helpers;
using JustinAngel.Neurons.Framework.Navigation;
using JustinAngel.UnitedNationsNews.AppCore;
using JustinAngel.UnitedNationsNews.Services;
using Newtonsoft.Json;

namespace JustinAngel.UnitedNationsNews.ItemViewMoels
{
    [JsonObject(MemberSerialization.OptIn)]
    public class ArticleItemViewModel : VViewModelBase, IShareableOnSocialNetworks
    {
        [JsonProperty]
        public string HtmlContent { get; set; }
        
        private string _title;
        [JsonProperty]
        public string Title
        {
            get { return _title; }
            set { _title = value.CleanHtml(); }
        }

        public string Content
        {
            get { return ArticleText; }
        }

        [JsonProperty]
        public Uri ImageUrl { get; set; }
        [JsonProperty]
        public Uri Link { get; set; }
        [JsonProperty]
        public string PublicationDateString { get; set; }


        public string ArticleText
        {
            get
            {
                return StripHtmlTags(
                    HtmlContent
                    .CleanHtml()
                    .Replace("<p>", Environment.NewLine + Environment.NewLine));

            }
        }

        //public string ArticleTextInOnePiece
        //{
        //    get
        //    {
        //        return 
        //            String.Join(
        //                string.Empty, 
        //                StripHtmlTags(
        //                    HtmlContent
        //                        .CleanHtml()
        //                        .Replace("<p>", Environment.NewLine)
        //                    ).Trim()
        //                .Split(new[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
        //                .Where(s => !string.IsNullOrEmpty(s.Trim()))
        //                .ToArray()

        //            );   

        //    }
        //}


        public string[] Paragraphs
        {
            get
            {
                return ArticleText.Split(new[] { Environment.NewLine + Environment.NewLine },
                                         StringSplitOptions.RemoveEmptyEntries)
                                  .Where(s => !string.IsNullOrEmpty(s.Trim()))
                                  .ToArray();
            }
        }

        public string StripHtmlTags(string text)
        {
            return Regex.Replace(text, @"<(.|\n)*?>", string.Empty);
        }

        public ICommand NavigateToArticle
        {
            get
            {
                return new RelayCommand(() =>
                    MessengerInstance.Send(new NavigateToMessage(PageSources.ArticlePage, this)));
            }
        }
    }
}